//
//  AppDelegate.m
//  Team9Project
//
//  Created by 朱嘉祺 on 15/10/12.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "AppDelegate.h"
#import "Team9TopViewController.h"
#import "Team9InspirationController.h"
#import "Team9CommunityViewController.h"
#import "Team9ExpertViewController.h"
#import "Team9UserViewController.h"
#import "Team9EveningType.h"
#import <AFNetworking.h>
#import <GPUImage.h>
#import "JQGuidanceVc.h"


@interface AppDelegate ()<UITabBarControllerDelegate>

@property(nonatomic, strong) UIView *netView;
@property(nonatomic, strong) UILabel *netLabel;
@property(nonatomic, strong) UIImageView *startImage;
@property(nonatomic, strong) GPUImageGaussianBlurFilter *blurFilter;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    [self.window makeKeyAndVisible];
    self.window.frame = [UIScreen mainScreen].bounds;
    Team9EveningType *evening = [Team9EveningType shareEvening];
//    self.window.backgroundColor = [UIColor whiteColor];
    evening.eveningFailling = [
                               [[NSUserDefaults standardUserDefaults] valueForKey:@"evening"] boolValue];
    
    [self startAnimation];
//    [UMSocialData setAppKey:@"507fcab25270157b37000010"];
    
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"firstStart"]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstStart"];
        //        NSLog(@"第一次启动");
        JQGuidanceVC *guiVC = [[JQGuidanceVC alloc] init];
        self.window.rootViewController = guiVC;
        
    } else {
        //        NSLog(@"不是第一次启动");
        
        [self reachability];
    }
    return YES;
}
#pragma mark 网络状态label
- (void)createNetView {
    self.netView = [[UIView alloc]
                    initWithFrame:CGRectMake(0, -64, self.window.bounds.size.width, 64)];
    self.netView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        self.netView.alpha = 1;
    [self.window addSubview:self.netView];
    
    UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc]
                                            initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    
    visualEffectView.frame = self.netView.bounds;
    visualEffectView.alpha = 1;
    
    // 讲设置好的UIVisualEffectView添加到要模糊的视图层上
    [self.netView addSubview:visualEffectView];
    
    self.netLabel = [[UILabel alloc]
                     initWithFrame:CGRectMake(50, 25, self.window.bounds.size.width - 50, 25)];
    self.netLabel.textColor = [UIColor whiteColor];
    [self.netView addSubview:self.netLabel];
    [self.window bringSubviewToFront:_netView];
}
#pragma mark 网络监听
- (void)reachability {
    
    AFNetworkReachabilityManager *reachabilityManager =
    [AFNetworkReachabilityManager sharedManager];
    // 开启网络监视器
//    NSLog(@"%@", reachabilityManager);
    [self createTabBar];
    [self createNetView];
    [reachabilityManager startMonitoring];
    [reachabilityManager
     setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
         
         [[NSNotificationCenter defaultCenter]postNotificationName:@"changeNet" object:nil];
         switch (status) {
             case AFNetworkReachabilityStatusUnknown:
                 //                NSLog(@"unknow");
                 [self internetCheck:@"未知网络"];
                 break;
             case AFNetworkReachabilityStatusNotReachable:
                 //                NSLog(@"notReachable");
                 [self internetCheck:@"无网络"];
                 break;
             case AFNetworkReachabilityStatusReachableViaWiFi:
//                                 NSLog(@"wifi");
                 [self internetCheck:@"Wifi网络"];
//                                 [reachabilityManager stopMonitoring];
                 break;
             case AFNetworkReachabilityStatusReachableViaWWAN:
                 //                NSLog(@"wan");
                 
                 [self internetCheck:@"蜂窝网络"];
//                                 [reachabilityManager stopMonitoring];
                 break;
             default:
                 break;
         }
     }];
    
}
#pragma mark 网络状态显示
- (void)internetCheck:(NSString *)statu {
    NSString *str = [NSString stringWithFormat:@"当前网络为%@", statu];
    //    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络变化提示"
    //    message:str delegate:self cancelButtonTitle:@"取消"
    //    otherButtonTitles:@"确定", nil];
    //    [alert show];
    NSLog(@"str : %@", str);
    self.netLabel.text = str;
    [UIView animateWithDuration:1
                     animations:^{
                         self.netView.frame =
                         CGRectMake(0, 0, self.window.bounds.size.width, 64);
                     }];
    [UIView animateWithDuration:1
                     animations:^{
                         [UIView setAnimationDelay:3.5];
                         self.netView.frame =
                         CGRectMake(0, -64, self.window.bounds.size.width, 64);
                     }];
}
#pragma mark 开机动画
- (void)startAnimation {
    CGRect frame = [UIScreen mainScreen].bounds;
    self.startImage = [[UIImageView alloc] initWithFrame:frame];
//    self.startImage.contentMode = UIViewContentModeScaleAspectFit;
    self.startImage.image = [UIImage imageNamed:@"open"];
    
    self.blurFilter = [[GPUImageGaussianBlurFilter alloc] init];
    self.blurFilter.blurRadiusInPixels = 0;
    self.startImage.image =
    [self.blurFilter imageByFilteringImage:self.startImage.image];
    
    // 讲设置好的UIVisualEffectView添加到要模糊的视图层上
    
    [self.window addSubview:self.startImage];
    dispatch_after(
                   dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)),
                   dispatch_get_main_queue(), ^{
                       [self shopAnimation];
                   });
}
#pragma mark 停止动画
- (void)shopAnimation {
    
    [UIView animateWithDuration:1.2
                     animations:^{
                         
                         self.startImage.alpha = 0;
                         self.startImage.bounds =
                         CGRectMake(0, 0, self.window.bounds.size.width * 1.8,
                                    self.window.bounds.size.height * 1.8);
                         
                     }];
    
    // 模糊
    [UIView animateWithDuration:0.0001
                     animations:^{
                         [UIView setAnimationDelay:0.4];
                         self.blurFilter.blurRadiusInPixels = 5;
                         self.startImage.image = [self.blurFilter
                                                  imageByFilteringImage:self.startImage.image];
                     }];
}
#pragma mark 禁止横屏
- (UIInterfaceOrientationMask)application:(UIApplication *)application
supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return UIInterfaceOrientationMaskPortrait;
}
#pragma mark 点击别的tabar回主页
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{

    if ([viewController isKindOfClass:[UINavigationController class]]) {
        [(UINavigationController *)viewController popToRootViewControllerAnimated:YES];
    }
    
}
#pragma mark 创建tabar
- (void)createTabBar {
    UITabBarController *taVC = [UITabBarController new];
    taVC.delegate = self;
    Team9TopViewController *topVC = [Team9TopViewController new];
    UINavigationController *topNaVC = [[UINavigationController alloc] initWithRootViewController:topVC];
    topNaVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"iconfont-zhuye-2"] tag:100];
    
    
    Team9InspirationController *insVC = [Team9InspirationController new];
    UINavigationController *insNaVC = [[UINavigationController alloc] initWithRootViewController:insVC];
    insNaVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"灵感" image:[UIImage imageNamed:@"iconfont-moshubang"] tag:101];
    
    Team9CommunityViewController *comVC = [Team9CommunityViewController new];
    UINavigationController *comNaVC = [[UINavigationController alloc] initWithRootViewController:comVC];
    comNaVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"社区" image:[UIImage imageNamed:@"iconfont-keai"] tag:102];
    
    Team9ExpertViewController *expVC = [Team9ExpertViewController new];
    UINavigationController *expNaVC = [[UINavigationController alloc] initWithRootViewController:expVC];
    expNaVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"专家" image:[UIImage imageNamed:@"iconfont-zhuanjiashenhe"] tag:103];
    
    Team9UserViewController *userVC = [Team9UserViewController new];
    UINavigationController *userNaVC = [[UINavigationController alloc] initWithRootViewController:userVC];
    userNaVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"iconfont-wode"] tag:104];
    
    taVC.viewControllers = @[topNaVC, insNaVC, comNaVC, expNaVC, userNaVC];
    
    
    self.window.rootViewController = taVC;
    [self.window bringSubviewToFront:self.startImage];
    
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    // Saves changes in the application's managed object context before the application terminates.
//    [self saveContext];
}



@end
